package com.wsm.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsm.common.annotation.RedisCache;
import com.wsm.common.config.BaseConfig;
import com.wsm.common.domain.ConfigDO;
import com.wsm.common.mapper.ConfigMapper;
import com.wsm.common.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统配置信息表
 *
 * @author wangshimin
 * @email wangshiminhome@foxmail.com
 * @date 2020-02-14 17:43:18
 * @description : 系统配置信息表
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, ConfigDO> implements ConfigService {
	@Autowired
	private ConfigMapper configMapper;
	@Autowired
	BaseConfig baseConfig;

	@Override
	@RedisCache(value = "config")
	public List<ConfigDO> list(Map<String, Object> map) {
		return configMapper.list(map);
	}

	@Override
	@RedisCache(value = "config")
	public int count(Map<String, Object> map) {
		return configMapper.count(map);
	}

	@Override
	@RedisCache(value = "config",flush = true)
	public int orSave(ConfigDO config) {
		baseConfig.setMap(config.getSysKey(), config.getSysValue());
		return configMapper.orSave(config);
	}
	
	@Override
	@RedisCache(value = "config",flush = true)
	public int update(ConfigDO config) {
		baseConfig.setMap(config.getSysKey(), config.getSysValue());
		return configMapper.orUpdate(config);
	}

	@Override
	@RedisCache(value = "config",flush = true)
	public int remove(Long id) {
		ConfigDO configDO = configMapper.selectById(id);
		if (null == configDO) {
			return 0;
		}
		baseConfig.deleteKey(configDO.getSysKey());
		return configMapper.remove(id);
	}

	@Override
	@RedisCache(value = "config",flush = true)
	public int batchRemove(Long[] ids) {
		return configMapper.batchRemove(ids);
	}


	@Override
	@RedisCache(value = "config",flush = true)
	public int updateUseFlag(Long id,Integer useFlag) {
		Map<String, Object> updateUseFlagMap = new HashMap<>(2);
		updateUseFlagMap.put("id",id);
		updateUseFlagMap.put("useFlag",useFlag);
		ConfigDO configDO = configMapper.selectById(id);
		if (null == configDO) {
			return 0;
		}
		baseConfig.deleteKey(configDO.getSysKey());
		return configMapper.updateUseFlag(updateUseFlagMap);
	}
}
